package com.lecture.java8.stack;

import com.lecture.java8.node.ForestNodeMerger;
import com.lecture.java8.node.INode;
import com.lecture.java8.node.Node;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class TreeOrder {
    public static void main(String[] args) {
        Node n0 = new Node();
        n0.setId(90L);
        n0.setParentId(0L);
        n0.setNodeName("父节点");

        Node n1 = new Node();
        n1.setId(1L);
        n1.setParentId(90L);
        n1.setNodeName("节点1");

        Node n2 = new Node();
        n2.setId(2L);
        n2.setParentId(90L);
        n2.setNodeName("节点2");

        Node n3 = new Node();
        n3.setId(3L);
        n3.setParentId(1L);
        n3.setNodeName("节点1-1");

        Node n4 = new Node();
        n4.setId(4L);
        n4.setParentId(1L);
        n4.setNodeName("节点1-2");

        Node n5 = new Node();
        n5.setId(5L);
        n5.setParentId(1L);
        n5.setNodeName("节点1-3");

        Node n6 = new Node();
        n6.setId(6L);
        n6.setParentId(2L);
        n6.setNodeName("节点2-1");

        Node n7 = new Node();
        n7.setId(7L);
        n7.setParentId(6L);
        n7.setNodeName("节点2-1-1");

        Node n8 = new Node();
        n8.setId(8L);
        n8.setParentId(6L);
        n8.setNodeName("节点2-1-2");

        Node n9 = new Node();
        n9.setId(9L);
        n9.setParentId(3L);
        n9.setNodeName("节点1-1-1");

        Node n10 = new Node();
        n10.setId(10L);
        n10.setParentId(3L);
        n10.setNodeName("节点1-1-2");

        Node n11 = new Node();
        n11.setId(11L);
        n11.setParentId(3L);
        n11.setNodeName("节点1-1-3");

        Node n12 = new Node();
        n12.setId(12L);
        n12.setParentId(2L);
        n12.setNodeName("节点2-2");

        Node n13 = new Node();
        n13.setId(13L);
        n13.setParentId(3L);
        n13.setNodeName("节点1-1-4");

        Node n14 = new Node();
        n14.setId(14L);
        n14.setParentId(12L);
        n14.setNodeName("节点2-2-2");

        Node n15 = new Node();
        n15.setId(15L);
        n15.setParentId(12L);
        n15.setNodeName("节点2-2-1");

        Node n16 = new Node();
        n16.setId(16L);
        n16.setParentId(15L);
        n16.setNodeName("节点2-2-1-1");

        List<Node> nodeList = new ArrayList<>();
        nodeList.add(n0);
        nodeList.add(n1);
        nodeList.add(n2);
        nodeList.add(n3);
        nodeList.add(n4);
        nodeList.add(n5);
        nodeList.add(n6);
        nodeList.add(n7);
        nodeList.add(n8);
        nodeList.add(n9);
        nodeList.add(n10);
        nodeList.add(n11);
        nodeList.add(n12);
        nodeList.add(n13);
        nodeList.add(n14);
        nodeList.add(n15);
        nodeList.add(n16);

        System.out.println(nodeList);

        List<Node> roots = ForestNodeMerger.merge(nodeList);

        System.out.println(nodeList.size());

        //顺序的树的线性结构
        List<Node> sortNodeList = new ArrayList<>();

        Deque<Node> nodeDeque = new ArrayDeque<>();
        //根节点入栈
        nodeDeque.push(roots.get(0));

        while (!nodeDeque.isEmpty()) {
            //取出栈顶元素
            Node pop = nodeDeque.pop();
            if (pop.getChildren() != null && pop.getChildren().size() != 0) {
                for (INode child : pop.getChildren()) {
                    nodeDeque.push((Node)child);
                }
            }
            sortNodeList.add(pop);
        }

        System.out.println(sortNodeList);
        System.out.println("\n\n\n\n");
        for (Node node : sortNodeList) {
            System.out.println(node.getNodeName());
        }
    }
}
